function buildHtml(labelName, labelArgv) {
    let result = `<${labelName}`;
    for (const i in labelArgv) {
        result += ` ${i}="${labelArgv[i]}"`;
    }
    result += `></${labelName}>`;
    return result;
}
function buildStyle(styleArgv) {
    let result = '';
    for (const i in styleArgv) {
        result += ` ${i}:${styleArgv[i]};`;
    }
    return result;
}
function replaceQQVideo2(content, articleWith) {
    const qqVideoRegex = /<embed.*?src="http:\/\/v.qq.com\/.*?\/([^\/]*?)\.html".*?width="(.*?)".*?height="(.*?)".*?\/>/img;
    const qqVideoWidth = articleWith;
    const qqVideoHeight = Math.ceil(qqVideoWidth / 1.5);
    const qqVideoSrc = `http://v.qq.com/iframe/player.html?vid=$1&width=${qqVideoWidth}&height=${qqVideoHeight}&auto=0`;
    const qqVideoStyle = buildStyle({
        'z-index': '1',
        height: `${qqVideoHeight}px !important`,
        width: `${qqVideoWidth}px !important`,
        overflow: 'hidden',
    });
    const qqVideoReplace = buildHtml(
        'iframe',
        {
            class: 'video_iframe',
            style: qqVideoStyle,
            height: `${qqVideoHeight}px`,
            width: `${qqVideoWidth}px`,
            frameborder: '0',
            'data-src': qqVideoSrc,
            src: qqVideoSrc,
            allowfullscreen: '',
            scrolling: 'no',
        },
    );
    content = content.replace(qqVideoRegex, qqVideoReplace);
    return content;
}
function replaceQQVideo(content, articleWith) {
    const qqVideoRegex = /<embed.*?src="http:\/\/static.video.qq.com\/TPout.swf\?vid=(.*?)".*?width="(.*?)".*?height="(.*?)".*?\/>/img;
    const qqVideoWidth = articleWith;
    const qqVideoHeight = Math.ceil(qqVideoWidth / 1.5);
    const qqVideoSrc = `http://v.qq.com/iframe/player.html?vid=$1&width=${qqVideoWidth}&height=${qqVideoHeight}&auto=0`;
    const qqVideoStyle = buildStyle({
        'z-index': '1',
        height: `${qqVideoHeight}px !important`,
        width: `${qqVideoWidth}px !important`,
        overflow: 'hidden',
    });
    const qqVideoReplace = buildHtml(
        'iframe',
        {
            class: 'video_iframe',
            style: qqVideoStyle,
            height: `${qqVideoHeight}px`,
            width: `${qqVideoWidth}px`,
            frameborder: '0',
            'data-src': qqVideoSrc,
            src: qqVideoSrc,
            allowfullscreen: '',
            scrolling: 'no',
        },
    );
    content = content.replace(qqVideoRegex, qqVideoReplace);
    return content;
}

function replaceImage(content, width) {
    if (!content) {
        return content;
    }
    const imageRegex = /<img(.*?)\/?>/ig;
    const imageReplace = `<img $1 style="width:${width}px"/>`;
    content = content.replace(imageRegex, imageReplace);
    return content;
}
function delImageSizeAttr(content) {
    if (!content) {
        return content;
    }
    content = content.replace('(<img[^>]*?)\s+width\s*=\s*\S+', '$1');
    content = content.replace('(<img[^>]*?)\s+height\s*=\s*\S+', '$1');
    return content;
}
function delImageSizeStyle(content) {
    if (!content) {
        return content;
    }
    content = content.replace('/style="(.*)"/', '');
    return content;
}

function resizeImagesWidth() {

}
module.exports = {
    jsonEscape: str => str.replace(/\n/g, '\\\\n').replace(/\r/g, '\\\\r').replace(/\t/g, '\\\\t').replace(/\\/g, '\\\\/'),
    html_decode: (str) => {
        let s = '';
        if (str == undefined || str.length == 0) return '';
        s = str.replace(/&amp;/g, '&');
        s = s.replace(/&lt;/g, '<');
        s = s.replace(/&gt;/g, '>');
        s = s.replace(/&nbsp;/g, ' ');
        s = s.replace(/&#39;/g, "\'");
        s = s.replace(/&quot;/g, '"');
        // s = s.replace(/<br\/>/g, "\n");

        return s;
    },
    html_encode: (str) => {
        if (!str) { return undefined; }
        let s = '';
        if (str == undefined || str.length == 0) return '';
        s = str.replace(/&/g, '&amp;');
        s = s.replace(/</g, '&lt;');
        s = s.replace(/>/g, '&gt;');
        s = s.replace(/ /g, '&nbsp;');
        s = s.replace(/\'/g, '&#39;');
        s = s.replace(/\"/g, '&quot;');
        s = s.replace(/\n/g, '<br/>');
        return s;
    },
    html_del: (str) => {
        if (str == undefined || str.length == 0) return '';
        let s = '';
        s = module.exports.html_decode(str);
        s = s.replace(/<[^>]+>/g, '');
        return s;// 去掉所有的html标记
    },

    parseHTML(content, width) {
        if (!content) content = '';
        content = replaceQQVideo(content, width);
        content = replaceQQVideo2(content, width);
        content = delImageSizeAttr(content);
        content = delImageSizeStyle(content);
        content = replaceImage(content, width);
        return content;
    },
    resizeImagesWidth: width => `${"var script = document.createElement('script');" +
      "script.type = 'text/javascript';" +
      'script.text = "function ResizeImages(screentWidth) { ' +
      '    var myimg,oldwidth,oldHeight;' +
      '    var maxwidth=screentWidth;var scrollHeight=document.body.scrollHeight;' +
      "    var ps = document.body.getElementsByTagName('p');" +
      '    for (j = 0; j < ps.length; j++) {' +
      "      if (ps[j].getElementsByTagName('iframe').length > 0) {" +
      "          var iframe = ps[j].getElementsByTagName('iframe')[0];" +
      '            if(iframe.width > maxwidth){' +
      '            oldwidth = iframe.width;' +
      '            oldHeight = iframe.height;' +
      '            iframe.width = maxwidth;' +
      '            iframe.height = oldHeight * (maxwidth/oldwidth);' +
      '            scrollHeight -=(oldHeight-iframe.height);' +
      '           }' +
      '      }' +
      '    }' +
      '    for(i=0;i <document.images.length;i++){' +
      '        myimg = document.images[i];' +
      '        if(myimg.width > maxwidth){' +
      '            oldwidth = myimg.width;' +
      '            oldHeight = myimg.height;' +
      '            myimg.width = maxwidth;' +
      '            myimg.height = oldHeight * (maxwidth/oldwidth);' +
      '            scrollHeight -=(oldHeight-myimg.height);' +
      '        }' +
      '    }' +
      'return scrollHeight;' +
      '}";' +
      "document.getElementsByTagName('head')[0].appendChild(script);" +
      "var s = document.createElement('link');" +
      "s.id='css_file';" +
      "s.rel='stylesheet';" +
      "s.type = 'text/javascript';" +
      "s.href='http://test008.xinyali.net/App/Front/Ios/react_001/Assets/app.css';" +
      "s.media='all';" +
      "document.getElementsByTagName('head')[0].appendChild(s);" +
      'ResizeImages('}${width});`,

    getImageOnWebView: () => "var script = document.createElement('script');" +
      "script.type = 'text/javascript';" +
      'script.text = ' +
      '"function clickFunction(image) { ' +
      'var iFrame;' +
      "iFrame = document.createElement('iframe');" +
      "iFrame.setAttribute('src', image.src);" +
      'document.body.appendChild(iFrame);' +
      'iFrame.parentNode.removeChild(iFrame);' +
      '};' +
      'function myFunction() { ' +
      "var img = document.getElementsByTagName('img');" +
      "for(var i=0;i<img.length;i++){img[i].setAttribute('onclick','clickFunction(this)');}" +
      '}";' +
      "document.getElementsByTagName('body')[0].appendChild(script);" +
      'myFunction();',
};
